﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kugar.Core.Configuration;
using Kugar.Core.ExtMethod;
using WS.FlowAuth.Web.BLL;
using WS.FlowAuth.Web.Data.DTO;
using WS.FlowAuth.Web.Data.EF;

namespace WS.FlowAuth.Web.Areas.Admin.Filters
{
    public class UserAuthorityAttribute: ActionFilterAttribute
    {
        private static int _defaultUserID =0;

        static UserAuthorityAttribute()
        {
            _defaultUserID = CustomConfigManager.Default.AppSettings.GetValueByName("DefaultUserID", "-1").ToInt();
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (_defaultUserID>0)
            {
                WebBLL.User.CurrentUser = WebBLL.User.GetUserByID(_defaultUserID);
            }

            if (WebBLL.User.CurrentUser==null)
            {
                WebBLL.User.CurrentUser = (base_User) filterContext.HttpContext.Session["CurrentUser"];
            }

            if (WebBLL.User.CurrentUser==null)
            {
                filterContext.Result=new RedirectResult("/Admin/Home/UserLogin?backurl=" + filterContext.RequestContext.HttpContext.Request.RawUrl);
                return;
            } 


            base.OnActionExecuting(filterContext);
        }
    }
}